package net.xi.news.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import javafx.geometry.Pos;
import net.xi.news.dao.SysUserMapper;
import net.xi.news.pojo.*;
import net.xi.news.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    private final NewsService newsService;

    private final TypeService typeService;

    private final TagService tagService;

    private final VideoService videoService;

    private final ImagesService imagesService;

    private final UserService userService;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysDictDataService sysDictDataService;

    @Autowired
    private LogService logService;

    @Autowired
    private RedisService redisService;

    public IndexController(NewsService newsService, TypeService typeService, TagService tagService, VideoService videoService, ImagesService imagesService, UserService userService) {
        this.newsService = newsService;
        this.typeService = typeService;
        this.tagService = tagService;
        this.videoService = videoService;
        this.imagesService = imagesService;
        this.userService = userService;
    }

    @GetMapping("/")
    //跳转到首页
    public String index(@PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC)
                                    @RequestParam(defaultValue = "1") Integer current, Model model) {
//        新闻分页
        Page<News> page = newsService.listNews(current);
//        前五视频
        Page<Video> videoPage = videoService.findVideoByPage();
//        大轮播图
        Page<Images> imagesPage = imagesService.imagesPage(Images.Type.INDEX);
//        禁毒百科轮播图
        Page<Images> wikipage = imagesService.imagesPage(Images.Type.WIKI);
//        给每个新闻查询作者信息
        for (News record : page.getRecords()) {

            SysUser sysUser = userService.findUserById(record.getUserid());
            record.setUser(sysUser);

            String notice_status = sysDictDataService.getTypeById(record.getType(), "newsType");
            record.setTypename(notice_status);
        }
        int i=1;
        for (Video record : videoPage.getRecords()) {
            record.setCont(i+"");
            System.out.println(i);
            i++;
        }



        model.addAttribute("imagespage",imagesPage);
        model.addAttribute("wikipage",wikipage);
        model.addAttribute("videopage",videoPage);
        model.addAttribute("page", page);
//        model.addAttribute("types",typeService.typeTop(5));
//        model.addAttribute("tags",tagService.tagTop(5));
        model.addAttribute("newsrecommend",newsService.newsTop(10));
//        hasPrevious  hasNext
//        是否存在上一页
        Boolean hasPrevious =false;
        if (page.getCurrent()>1){
            hasPrevious=true;
        }
        model.addAttribute("hasPrevious", hasPrevious);
//        是否存在下一页
        Boolean hasNext =false;
        if (page.getCurrent()<page.getPages()){
            hasNext=true;
        }
        model.addAttribute("hasNext", hasNext);
        logService.LogAdd(1);
        return "index";
    }


    /**
     * 数据可视化
     * @return
     */
    @GetMapping("/datavisualization")
    public String DataVisualization(Model model){
        System.out.println("进入数据可视化");
//        此处增加网站浏览量
//        统计当月数据
//        所有数据只保存30天，每次操作缓存，每10分钟持久化到数据库一次
        logService.LogAdd(2);

        List<Log> logByPage = logService.findLogByPage(1);
        List<Log> logByPage1 = logService.findLogByPage(2);
        List<Integer> list=new ArrayList<>();
        List<Integer> list2=new ArrayList<>();
        List<String> namelist=new ArrayList<>();
//        网站浏览量
        for (Log log : logByPage) {
            list.add(log.getSum());
            namelist.add(log.getName());
        }

//        数据大屏浏览量
        for (Log log : logByPage1) {
            list2.add(log.getSum());
        }

        model.addAttribute("logindex",list);
        model.addAttribute("logksh",list2);
        model.addAttribute("namelist",namelist);
        return "echart/index";
    }

    @PostMapping("/search")
    //跳转到搜索结果页
    public String Search(@PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC)
                                 Pageable pageable,String type,
                         @RequestParam String query, Model model) {
//      1 2 新闻  3视频
        if ("3".equals(type)){
//            如果type=3 则搜索视频
            Page<Video> videoPage = videoService.listsearchVideo(query, pageable);
            for (Video record : videoPage.getRecords()) {
                SysUser sysUser = sysUserMapper.selectById(record.getUserid());
                record.setUser(sysUser);
                record.setDescription(record.getTitle());
                String notice_status = sysDictDataService.getTypeById(record.getType().toString(), "videotype");
                record.setTypename(notice_status);
            }
            PageImpl<Video> videos = new PageImpl<>(videoPage.getRecords(), pageable, videoPage.getTotal());
            model.addAttribute("page", videos);
            model.addAttribute("total", videoPage.getTotal());
            model.addAttribute("querys",query);
        }else {
//            搜索新闻
            Page<News> o = newsService.listsearchNews(query, pageable);
            for (News record : o.getRecords()) {
                SysUser sysUser = sysUserMapper.selectById(record.getUserid());
                record.setUser(sysUser);
                String notice_status = sysDictDataService.getTypeById(record.getType(), "newsType");
                record.setTypename(notice_status);
            }
            PageImpl<News> news = new PageImpl<>(o.getRecords(), pageable, o.getTotal());
            System.out.println(news);
            model.addAttribute("page", news);
            model.addAttribute("total", o.getTotal());
            model.addAttribute("querys",query);
        }
        model.addAttribute("type",type);

        return "search";
    }



    @GetMapping("/news/{id}")
    //跳转到新闻详情页
    public String news(@PathVariable String id,Model model){
        News convert = newsService.getConvert(id);
        SysUser sysUser = sysUserMapper.selectById(convert.getUserid());
        String notice_status = sysDictDataService.getTypeById(convert.getType(), "newsType");
        convert.setTypename(notice_status);
        model.addAttribute("news", convert);
        model.addAttribute("user",sysUser);
//        累加一次浏览量
        convert.setViews(convert.getViews()+1);
        newsService.updateById(convert);
        return "news";
    }


    @GetMapping("/about")
    //跳转到关于页
    public String about(){
        return "about";
    }



    @GetMapping("/newslist")
    //跳转到新闻列表页
    public String NewsList(@PageableDefault( size = 10,sort = {"updateTime"},direction = Sort.Direction.DESC)
                                 Pageable pageable,
                          String query, Model model) {
        Page<News> o = newsService.listsearchNews(query, pageable);
        for (News record : o.getRecords()) {
            SysUser sysUser = sysUserMapper.selectById(record.getUserid());
            record.setUser(sysUser);
            String notice_status = sysDictDataService.getTypeById(record.getType(), "newsType");
            record.setTypename(notice_status);
        }
        PageImpl<News> news = new PageImpl<>(o.getRecords(), pageable, o.getTotal());
        System.out.println(news);
        model.addAttribute("page", news);
        model.addAttribute("total", o.getTotal());
        model.addAttribute("querys",query);
        return "newslist";
    }

    @GetMapping("/videolist")
    //跳转到视频列表页
    public String videoList(@PageableDefault( size = 10,sort = {"updateTime"},direction = Sort.Direction.DESC)
                                   Pageable pageable,
                           String query, Model model) {
        Page<Video> videoPage = videoService.listsearchVideo(query, pageable);
        for (Video record : videoPage.getRecords()) {
            SysUser sysUser = sysUserMapper.selectById(record.getUserid());
            record.setUser(sysUser);
            String notice_status = sysDictDataService.getTypeById(record.getType().toString(), "videotype");
            record.setTypename(notice_status);
        }
        PageImpl<Video> videos = new PageImpl<>(videoPage.getRecords(), pageable, videoPage.getTotal());
        System.out.println(videos);
        model.addAttribute("page", videos);
        model.addAttribute("total", videoPage.getTotal());
        model.addAttribute("querys",query);
        return "videolist";
    }

}